package service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import common.BeanUtils;

import service.LightsControl;
import bean.Intersection;
import bean.Point;
import bean.TrafficLights;
import bean.TrafficLights.Lights;

public class BasicLightsControl implements LightsControl {

	private boolean stop = false;

	private List<TrafficLights> trafficLights;

	private Map<Point, TrafficLights.Lights> lights = new HashMap<Point, TrafficLights.Lights>();

	public BasicLightsControl(List<Intersection> ins) {
		if (ins == null)
			return;
		trafficLights = new ArrayList<TrafficLights>();
		for (Intersection in : ins) {
			if (in.size() < 6)
				continue;
			TrafficLights rl = new TrafficLights(TrafficLights.lightsCount(),
					in);
			trafficLights.add(rl);
			if (in.size() == 8) {
				// 十字路口
				for (int i = 0; i < 8; i += 2) {
					lights.put(in.get(i), rl.getLight(i));
				}
			} else if (in.size() == 6) {
				// 三岔路口
				for (int i = 0; i < 8; i += 2) {
					if (in.get(i) == null)
						continue;
					lights.put(in.get(i), rl.getLight(i));
				}
			}
		}
	}

	@Override
	public void run() {
		for (TrafficLights rl : trafficLights) {
			/**
			 * 1.南北方向直行20s 2.东西方向直行20s 3.南北方向左行20s 4.东西方向左行20s
			 */
			List<TrafficLights.ControlCircle> ccs = new ArrayList<TrafficLights.ControlCircle>(
					4);
			int timer = 20;
			ccs.add(rl.new ControlCircle(rl.lights[0], Lights.FORWARD, timer));
			ccs.add(rl.new ControlCircle(rl.lights[1], Lights.FORWARD, timer));
			ccs.add(rl.new ControlCircle(rl.lights[0], Lights.LEFT, timer));
			ccs.add(rl.new ControlCircle(rl.lights[1], Lights.LEFT, timer));
			rl.setCtrlO(ccs);
			rl.begin();
		}
		while (!stop) {
			// 输出些什么...
			TrafficLights rl = trafficLights.get(2);
			Lights l0 = rl.lights[0];
			Lights l1 = rl.lights[1];
			Lights l2 = rl.lights[2];
			Lights l3 = rl.lights[3];
			Lights ln1 = getCurrentTrafficLight(rl.intersection.getS1());
			System.out.println("S1^^^N2:"
					+ check(rl.intersection.getS1(), rl.intersection.getN2()));
			System.out.println("S1<<<W2:"
					+ check(rl.intersection.getS1(), rl.intersection.getW2()));
			System.out.println("N1^^^S2:"
					+ check(rl.intersection.getN1(), rl.intersection.getS2()));
			System.out.println("N1<<<E2:"
					+ check(rl.intersection.getN1(), rl.intersection.getE2()));
			System.out.println("E1^^^W2:"
					+ check(rl.intersection.getE1(), rl.intersection.getW2()));
			System.out.println("E1<<<S2:"
					+ check(rl.intersection.getE1(), rl.intersection.getS2()));
			System.out.println("--------------S1:" + (ln1.left ? "<" : "x")
					+ (ln1.forward ? "^" : "x") + ">--------------");
			System.out.println("第[" + rl.intersection.getId() + "]个路口交通情况");
			System.out.println("     |  | |    |");
			System.out.println("     |  V | " + (l0.left ? "<" : "x")
					+ (l0.forward ? "^" : "x") + ">|");
			System.out.println("-----  N1   N2  -----");
			System.out.println("" + (l3.left ? "<" : "x")
					+ (l3.forward ? "^" : "x") + ">W2           E1<---");
			System.out.println("-----           -----");
			System.out.println("-->W1           E2" + (l1.left ? "<" : "x")
					+ (l1.forward ? "^" : "x") + ">");
			System.out.println("-----  S2   S1  -----");
			System.out.println("     | " + (l2.left ? "<" : "x")
					+ (l2.forward ? "^" : "x") + ">|  ^ |");
			System.out.println("     |    |  | |");
			System.out.println("");
			try {
				Thread.sleep(15 * 1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	public bean.TrafficLights.Lights getCurrentTrafficLight(Point p) {
		return lights.get(p);
	}

	@Override
	public void stop() {
		this.stop = true;
	}

	@Override
	public boolean check(Point p1, Point p2) {
		Lights l = getCurrentTrafficLight(p1);
		synchronized (l) {
			int dir = BeanUtils.direction(p1, p2);
			if ((l.forward && dir == Lights.FORWARD)
					|| (l.left && dir == Lights.LEFT)
					|| (l.right && dir == Lights.RIGHT)) {
				return true;
			}
		}
		return false;
	}

}
